package utils.anime
{
	import com.codezen.helper.DatabaseAsync;

	/**
	 * Episodes database 
	 * @author Light
	 * 
	 */
	public class AniEpisodesDB extends DatabaseAsync
	{
		
		public function AniEpisodesDB()
		{
			file = 'anime.db';
			createStatement = "CREATE TABLE IF NOT EXISTS episodes (" + 
				"    id INTEGER PRIMARY KEY, " +
				"    aid TEXT, " +
				"    season NUMERIC CHECK (season > -1)," +
				"    num NUMERIC CHECK (num > -1)," +
				"    title TEXT, " +
				"    jtitle TEXT, " +
				"    image TEXT, " + 
				"    video TEXT, " +
				"    snd1 TEXT, " +
				"    snd2 TEXT, " +
				"    sub1 TEXT, " +
				"    sub2 TEXT, " +
				"    tmark TEXT, " +
				"    view BOOL, " +
				"    serial_id TEXT, " +
				"    serial_title TEXT, " +
				"    serial_etitle TEXT, " +
				"    serial_jtitle TEXT, " +
				"    serial_simg TEXT, " +
				"    serial_bimg TEXT, " +
				"    serial_info TEXT " +
				");";
			doInit();
			
		}
		
		public function saveEpisode(ep:AniEpisode):void{
			// id season num title image video tmark view serial_id serial_title serial_original serial_simg serial_bimg serial_info
			var query:String = "INSERT OR REPLACE INTO episodes (id, aid, season, num, title, jtitle, image, video, " +
					"snd1, snd2, sub1, sub2, tmark, view, serial_id, serial_title, serial_etitle, serial_jtitle, " +
					"serial_simg, serial_bimg, serial_info) " +
					"VALUES " +
					"(:id, :aid, :season, :num, :title, :jtitle, :image, :video, :snd1, :snd2, :sub1, :sub2, :tmark, :view, " +
					":serial_id, :serial_title, :serial_etitle, :serial_jtitle, :serial_simg, :serial_bimg, :serial_info) ";
			
			var params:Array = [
					[":id", ep.id],
					[":aid", ep.aid],
					[":season", ep.season],
					[":num", ep.num],
					[":title", ep.title],
					[":jtitle", ep.jtitle],
					[":image", ep.image],
					[":video", ep.video],
					[":snd1", ep.snd1],
					[":snd2", ep.snd2],
					[":sub1", ep.sub1],
					[":sub2", ep.sub2],
					[":tmark", ep.tmark],
					[":view", ep.view],
					[":serial_id", ep.serial_id],
					[":serial_title", ep.serial_title],
					[":serial_etitle", ep.serial_etitle],
					[":serial_jtitle", ep.serial_jtitle],
					[":serial_simg", ep.serial_simg],
					[":serial_bimg", ep.serial_bimg],
					[":serial_info", ep.serial_info]
				];
			
			queryDatabase(query, params);
		}
		
		public function checkEpisode(ep:AniEpisode):void{
			// id season num title image video tmark view serial_id serial_title serial_original serial_simg serial_bimg serial_info
			var query:String = "SELECT * FROM episodes WHERE id = :id AND video NOT LIKE '%[[local=%' LIMIT 1";
			var params:Array = [
					[":id", ep.id]
				];
			
			queryDatabase(query, params);
		}
		
		public function delEpisode(ep:AniEpisode):void{
			// id season num title image video tmark view serial_id serial_title serial_original serial_simg serial_bimg serial_info
			var query:String = "DELETE FROM episodes WHERE id = :id";
			var params:Array = [
				[":id", ep.id]
			];
			
			queryDatabase(query, params);
		}
		
		public function removeSeries(s:AniSerial):void{
			var query:String = "DELETE FROM episodes WHERE serial_id = :serial_id";
			var params:Array = [
				[":serial_id", s.id]
			];
			
			queryDatabase(query, params);
		}
		
		public function getSeriesList():void{
			var query:String = "SELECT DISTINCT serial_id, serial_title, " +
				"serial_etitle, serial_simg, " +
				"serial_bimg, serial_info " +
				"FROM episodes";
			
			queryDatabase(query);
		}
		
		public function getSavedEpisodesList():void{
			var query:String = "SELECT * " +
				"FROM episodes WHERE " +
				"video LIKE '%[[local=%'";

			queryDatabase(query);
		}
	}
}